﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using NHibernate;
using NHibernate.Criterion;

namespace DomainModel.Concrete
{
    public class TagsRepository : ITagsRepository
    {
        public IList<Tag> GetAll()
        {
            ICriteria criteria = SessionManager.CurrentSession.CreateCriteria(typeof(Tag));
            return criteria.List<Tag>();
        }

        public Tag Get(int id)
        {
            return SessionManager.CurrentSession.Get<Tag>(id);
        }

        public IList<Tag> GetByName(string name)
        {
            ICriteria criteria = SessionManager.CurrentSession.CreateCriteria(typeof(Tag));
            return criteria.List<Tag>().Where(x=>x.TagName == name).ToList();
        }



        public void Save(Tag item)
        {
            ISession session = SessionManager.CurrentSession;
            session.BeginTransaction();
            session.Save(item);
            session.Transaction.Commit();
        }

        public void Merge(Tag item)
        {
            ISession session = SessionManager.CurrentSession;
            session.BeginTransaction();
            session.Merge(item);
            session.Transaction.Commit();
        }

        public void Delete(int id)
        {
            SessionManager.CurrentSession.Delete(Get(id));
        }

     
    }
}
